from util import Util

class MockOrder(object):
    def __init__(self, *args, **kwargs):
        if args is not None and len(args) > 0:
            self.__orderId = args[0]
            self.__orderNo = args[1]
            self.__price = args[2]
        elif kwargs is not None and len(kwargs) > 0:
            self.__orderId = kwargs["orderId"]
            self.__orderNo = kwargs["orderNo"]
            self.__price = kwargs["price"]
        else:
            self.__orderId = None
            self.__orderNo = "orderNo1"
            self.__price = 256.03
            
    @property
    def orderId(self):
        return self.__orderId
    @orderId.setter
    def orderNo(self, value):
        self.__orderId = value

    @property
    def orderNo(self):
        return self.__orderNo 
    @orderNo.setter
    def orderNo(self, value):
        self.__orderNo = value
    
    @property 
    def price(self):
        return self.__price
    @price.setter
    def price(self, value):
        self.__price = value

    def __str__(self):
        return "[orderId:%d, orderNo:%s, price:%f]" % (self.__orderId, self.__orderNo, self.__price)


from sqliteStorage import SqliteStorage
class MockOrderDao(SqliteStorage.ISqliteDao):
    def __init__(self, pojo):
        self.pojo = pojo
        self.__schema = None #TODO: 
        self.__table = "tbl_order"

    def createSql(self, **kwargs):
        return "create table if not exists %s (orderId integer primary key, orderNo text, price real)" % (self.__table)
    def dropSql(self, **kwargs):
        return "drop table %s" % self.__table

    def insertSql(self, **kwargs):
        return "insert into %s values(NULL, %s, %s)" % (self.__table, Util.sqlString(self.pojo.orderNo), Util.sqlString(self.pojo.price))
    def deleteSql(self, **kwargs):
        return "delete from %s " % self.__table
    def updateSql(self, **kwargs):
        return "update %s set orderNo=%s, price=%s where orderNo = %s" % (self.__table, Util.sqlString(self.pojo.orderNo), Util.sqlString(self.pojo.price), Util.sqlString(self.pojo.orderNo))

    def querySql(self, **kwargs):
        if super(MockOrderDao, self).isCmd("all", kwargs):
            return "select orderId, orderNo, price from %s " % self.__table
        else:
            print("unknow cmd:[%s], kwargs:%s" % (cmd, kwargs))
    def toPojo(self, obj, **kwargs):
        if super(MockOrderDao, self).isCmd("all", kwargs):
            result = [] 
            for i in obj:
                result.append(MockOrder(*i))
            return result
        else:
            print("unknow cmd:[%s], kwargs:%s" % (cmd, kwargs))



def testSqliteDao():
    dao = MockOrderDao(MockOrder())
    ss = SqliteStorage("test.db")
    ss.create(dao)
    ss.insert(dao)
    print(ss.query(dao, cmd="all"))
    ss.update(dao)
    ss.delete(dao)
    #ss.query(dao, where="orderNo=1")
    ss.drop(dao)
    ss.close()

"""
class MockOrdersDao(object):
    def __init__(self, orders=[]):
        pass

    def insert(self, **kwargs):
        pass
    def delete(self, **kwargs):
        pass
    def update(self, **kwargs):
        pass
    def query(self, **kwargs):
        pass
#end class


from storageEngine import SqliteBatchStorage
def testOrders():
    orders = []
    orders.append(MockOrder())
    orders.append(MockOrder())
    orders.append(MockOrder())
    orders.append(MockOrder())
    orders.append(MockOrder())
    orders.append(MockOrder())
    orders.append(MockOrder())

    dao = MockOrdersDao(orders)
    sbs = SqliteBatchStorage(":memory")
    sbs.insert(dao)
    sbs.update(dao)
    sbs.delete(dao)
    sbs.query(dao)
"""
if __name__ == "__main__":
    testSqliteDao()
